package com.farm.wcp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.farm.authority.service.OrganizationServiceInter;
import com.farm.authority.service.UserServiceInter;
import com.farm.core.ParameterService;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.page.ViewMode;
import com.farm.learn.utils.WcpSycner.WcpKnow;
import com.farm.parameter.FarmParameterService;
import com.farm.wcp.client.WcpClient;
import com.farm.web.WebUtils;
import com.farm.wuc.events.WucEventDefinitions;
import com.farm.wuc.events.WucEventModle;
import com.farm.wuc.events.WucEventRole;
import com.wcp.question.service.QuestionServiceInter;
import com.wts.client.WtsAuthClient;
import com.wts.client.WtsClient;
import com.wts.client.WtsExamClient;
import com.wts.client.utils.WtcJsonMap;
import com.wuc.client.WucEventClient;

/**
 * 考试系统应用接口
 * 
 * @author wangdogn
 *
 */
@RequestMapping("/wtsweb")
@Controller
public class WtsWebController extends WebUtils {

	@Resource
	private UserServiceInter userServiceImpl;
	@Resource
	private QuestionServiceInter questionServiceImpl;
	@Resource
	private OrganizationServiceInter organizationServiceImpl;
	private static final Logger log = Logger.getLogger(WtsWebController.class);

	public static String getThemePath() {
		return FarmParameterService.getInstance().getParameter("config.sys.web.themes.path");
	}

	/**
	 * 登录到wts
	 */
	@RequestMapping("/loadUser")
	public ModelAndView home(HttpSession session, HttpServletRequest request) {
		ParameterService conf = FarmParameterService.getInstance();
		WtsClient wts = new WtsClient(conf.getParameter("config.lwts.api.base.url"),
				conf.getParameter("config.lwts.api.secret"), conf.getParameter("config.lwts.api.loginname"),
				conf.getParameter("config.lwts.api.password"));
		LoginUser user = getCurrentUser(session);
		String goUrl = wts.getUrlByPop(user.getLoginname(),
				conf.getParameter("wlp:config.lwts.api.guest.url") + "/login/back.html", false);
		return ViewMode.getInstance().putAttr("goUrl", goUrl).returnModelAndView("toolpage/indexResult");
	}

	/**
	 * 获取所有答题室
	 * 
	 * @param classid
	 * @param session
	 * @return
	 */
	@RequestMapping("/getAllRooms")
	@ResponseBody
	public Map<String, Object> tempClass(String majorid, HttpSession session) {
		try {
			ParameterService conf = FarmParameterService.getInstance();
			WtsExamClient wts = new WtsExamClient(conf.getParameter("config.lwts.api.base.url"),
					conf.getParameter("config.lwts.api.secret"), conf.getParameter("config.lwts.api.loginname"),
					conf.getParameter("config.lwts.api.password"));
			List<WtcJsonMap> rooms = wts.getRooms(null, null);
			List<Map<String, String>> back = new ArrayList<Map<String, String>>();
			for (WtcJsonMap node : rooms) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("ID", node.getString("ID"));
				map.put("NAME", node.getString("NAME"));
				back.add(map);
			}
			return ViewMode.getInstance().putAttr("DATA", back).returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.getMessage(), e).returnObjMode();
		}
	}

}
